package com.alarmgame.alert;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alarmgame.R;
/**
 * 
 * Alarm Game Alert.
 * 
 * @author EJ
 *
 */
public class AlarmGameAlert extends Activity {

	private MediaPlayer mPlayer = null;
    public static String SERVERIP = "10.0.2.15";
    public static final int SERVERPORT = 8181;
	private static final String OFF = "off";
    private ServerSocket serverSocket;    

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_layout);					

		// Create a dialog that the user must dismiss
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setMessage("Detener Alarma");

		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlarmGameAlert.this, "Dentro del onClick()", Toast.LENGTH_LONG).show();
				SERVERIP = getLocalIpAddress();
				Toast.makeText(getBaseContext(), "IP address: " + SERVERIP, Toast.LENGTH_LONG).show();
				Uri uriUrl = Uri.parse("http://www.google.com.mx/");
				Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
				startActivity(launchBrowser);
				try {
					serverSocket = new ServerSocket(SERVERPORT);
					while (true) {
						Socket client = serverSocket.accept();
						try {
	                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
	                        String line = null;
	                        while ((line = in.readLine()) != null) {
	                            Log.d("ServerActivity", line);	                            
	                        }
	                        if(line != null){
	                        	 if(line.equals(OFF)){	                        
	 	                        	break;
	 	                        }
	                        }
	                    } catch (Exception e) {	                        	                          
	                        Log.d("ServerActivity", e.getMessage());
	                        Toast.makeText(getBaseContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
	                    }
					}					
				} catch (IOException e) {
					Log.d("ServerActivity", e.getMessage());
                    Toast.makeText(getBaseContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
				}
				mPlayer.stop();
				Toast.makeText(getBaseContext(), "Done", Toast.LENGTH_LONG).show();
				finish();
			}
		});
		alertDialog.setIcon(R.drawable.icon);
		alertDialog.show();
	}	

	@Override
	protected void onPostResume() {
		super.onPostResume();
		mPlayer = MediaPlayer.create(getBaseContext(), R.raw.alarm_clock1);
		mPlayer.start();
		mPlayer.setLooping(Boolean.TRUE);
	}
	/**
	 * Retrieve the IP address from NetworkInterface.
	 * @return
	 */
    private String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                }
            }
        } catch (SocketException ex) {
            Log.e("ServerActivity", ex.toString());
        }
        return null;
    }
}
